<template>
  <div class="app-container">
    <el-card>
      <el-button type="primary" icon="el-icon-plus" @click="openDialog">添加分类信息</el-button>
    </el-card>
    <el-card style="margin-top: 1rem">
      <el-table :data="classifyList" stripe>
        <el-table-column prop="classifyId" label="分类ID" />
        <el-table-column prop="classifyName" label="分类名" />
        <el-table-column prop="classifyParentId" label="分类父ID" />
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="openDialog(scope.row)" />
            <el-button type="danger" icon="el-icon-delete" @click="deleteRow(scope.row.classifyId)" />
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="分类信息"
        :destroy-on-close="true"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="dialogVisible"
        width="50%"
        @close="refresh"
      >
        <add-or-update-classify
          :key="dialogVisible"
          :classify-id="classifyId"
          :classify-name="classifyName"
          @updateDialog="updateDialog"
        />
      </el-dialog>
      <el-pagination
        style="margin-top: 20px;"
        background
        :current-page="currentPage"
        :page-sizes="[10, 25, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import AddOrUpdateClassify from '@/views/admin/setting/classify/components/AddOrUpdateClassify'
import { deleteClassify, listClassifyAdmin } from '@/api/classify'

export default {
  components: {
    AddOrUpdateClassify
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalNum: 0,
      classifyId: -1,
      classifyName: '',
      dialogVisible: false,
      classifyList: [
        { classifyId: 1001, classifyName: '数论', classifyParentId: 0 }
      ]
    }
  },
  created() {
    this.listClassifyAdmin()
  },
  methods: {
    listClassifyAdmin() {
      listClassifyAdmin({ current: this.currentPage, size: this.pageSize }).then(res => {
        this.totalNum = parseInt(res.data.total)
        this.classifyList = res.data.records
      })
    },
    deleteRow(classifyId) {
      this.$confirm('此操作将永久删除分类数据，是否继续删除？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        deleteClassify({ classifyId: classifyId }).then(res => {
          if (res.data) {
            this.$message.success(res.message)
            this.listClassifyAdmin()
          }
        })
      })
    },
    updateDialog(flag) {
      this.dialogVisible = flag
    },
    openDialog(row) {
      this.classifyName = row.classifyName
      this.classifyId = row.classifyId
      this.dialogVisible = true
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.listClassifyAdmin()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.listClassifyAdmin()
    },
    refresh() {
      this.listClassifyAdmin()
    }
  }
}
</script>

<style scoped>

</style>
